package com.zerone.pay.common;

import com.acooly.core.utils.Strings;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

/**
 * @author 木鱼 muyu@yiji.com
 * @version 2017/4/28
 */
public class PayClient {

    /**
     * 签名工具
     *
     * @param key 商户私钥
     * @param url 签名URL
     * @return
     */
    public static String sign(String key,String url) {
        String paramUrl = url;
        if(url.startsWith("http")){
             paramUrl = url.split("\\?")[1];
        }

        String sortParam = Arrays.stream(paramUrl.split("&")).filter((String s) ->
            s.split("=").length>0&&!Strings.equals(s.split("=")[0],"sign")
        ).sorted(Comparator.comparing(s -> s.substring(0))).map(String::toString)
                .collect(Collectors.joining("&"));
        System.out.println("被加密字符串："+sortParam);
        return DigestUtils.md5Hex(sortParam + key);
    }
}
